using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml.Packaging;
using SharePoint.Security.DocHelpers.ExportFormats;

namespace SharePoint.Security.ContentScanner.Engine.Analyzers
{
    public abstract class BaseDocumentScanner : IDocumentScanner
    {
        protected BaseDocumentScanner()
        {
            DocQualifier = "Word Document";
            Results = new List<ScanResult>();
        }
        protected string DocQualifier;

        public List<ScanResult> Results { get; set; }

        protected const int MaxToDisplay = 50;

        public abstract List<ScanResult> Scan(string tempDocPath, List<Pattern> patterns, string fileFullPath);

        protected void SearchRegExpString(string text, List<Pattern> patterns, string displayFileName, string prefix, string fileType)
        {
            if (text == null || patterns == null)
            {
                throw new ArgumentNullException("Content or regular expression is null.");
            }

            foreach (var item in patterns)
            {
                //bool found = false;
                var reg = new Regex(item.RegEx, RegexOptions.IgnoreCase);

                var m = reg.Match(text);
                while (m.Success)
                {
                    int length = 0;
                    while (m.Index + length < text.Length && length < MaxToDisplay)
                    {
                        if (text[m.Index + length] == ' ' ||
                            text[m.Index + length] == ',' ||
                            text[m.Index + length] == '\r')
                        {
                            break;
                        }
                        length++;
                    }

                    var subString = text.Substring(m.Index, length);
                    while (subString.EndsWith("\r"))
                    {
                        subString = text.Substring(m.Index, --length);
                    }

                    
                    var result = new ScanResult
                    {
                        FileFullName = displayFileName,
                        FileType = fileType,
                        MatchingContent = text.Substring(m.Index, length),
                        MoreInfo = prefix,
                        Pattern = string.IsNullOrEmpty(item.Name) ? item.RegEx : item.Name + " " + item.RegEx 
                    };

                    Results.Add(result);
                    m = m.NextMatch();
                }
            }
        }

        protected void CheckProperties(OpenXmlPackage propContainer, List<Pattern> patterns, string displayFileName, string prefix, string fileType)
        {
            foreach (var item in propContainer.PackageProperties.GetType().GetProperties())
            {
                string name = item.Name;
                var propVal = item.GetValue(propContainer.PackageProperties, null);
                if (propVal != null && propVal.ToString() != "")
                {
                    SearchRegExpString(propVal.ToString(), patterns, displayFileName, String.Format("{0}[{1}]", prefix, name), fileType);
                }
            }
        }
    }
}